/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package server.server.senderEvent;

import protocolHandler.ProtoMessageParser;
import java.util.ArrayList;
import java.util.HashMap;

/**
 *
 * @author Александр Емельянов (mr.lex91@gmail.com)
 */
public class SenderProducer {
    private HashMap<String, SenderListener> list=new HashMap<String, SenderListener>();
    public void addSenderListener(SenderListener listener)
    {
        list.put(listener.getNickName(), listener);
    }
    public SenderListener[] getSenderListener()
    {
        return (SenderListener[]) list.values().toArray();
    }

    public void removeSenderListener(String nickName)
    {
        list.remove(nickName);
    }

    public void Send(String msg)
    {
        SenderEvent ev = new SenderEvent(this,msg);
        ProtoMessageParser parser=new ProtoMessageParser();
        parser.Parse(msg);
        for(SenderListener listener : list.values())
        {
            if (!listener.getNickName().equalsIgnoreCase(parser.getHeaderVal("Address"))) listener.Send(ev);
        }
    }
    public void SendTo(String msg, String nickName)
    {
        SenderEvent ev = new SenderEvent(this,msg);
        if (list.containsKey(nickName)) list.get(nickName).Send(ev);

    }
    public void SendRosterTo(String nickName)
    {
        String roster="Roster\nSize: "+list.size()+"\nFolowers: ";
        int len=roster.length();
        for(SenderListener listener : list.values())
        {
            roster+=((roster.length()==len)?"":"\n")+listener.getNickName();
        }
        SendTo(roster, nickName);
    }
    public boolean ConteinsConnection(String nickName)
    {
        return list.containsKey(nickName);
    }
}
